<template>
    <el-dialog v-model="dialogVisible" title="库存盘点" min-width="30%" draggable
    
    >

        <el-form
        label-position="top"
        >

            <el-form-item label="仓库名称">
                <el-input size="large" :placeholder="'仓库名称'" v-model="bodyData.warehouse.name" disabled></el-input>
            </el-form-item>

            <el-form-item label="商品名称">
                <el-input size="large" :placeholder="'商品名称'" v-model="bodyData.goods.name" disabled></el-input>
            </el-form-item>

            <el-form-item label="当前库存">
                <el-input size="large" :placeholder="'仓库名称'" v-model="bodyData.stock" disabled></el-input>
            </el-form-item>

            <!-- 盘点数量 -->
            <el-form-item label="盘点数量">
                <el-input size="large" 
                type="number"
                :placeholder="'盘点数量'" v-model="requestParam.newStock"></el-input>
            </el-form-item>

            <!-- 备注 -->
            <el-form-item label="备注">
                <el-input size="large" :placeholder="'备注'" v-model="requestParam.remark"></el-input>
            </el-form-item>



        </el-form>

        <template v-slot:footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit()">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>


<script lang="ts" setup>
import { ref, defineEmits, defineExpose } from 'vue';
import $kt from '@/vueKantboot';
import { ElLoading, ElMessage, ElNotification } from 'element-plus'
import { de, fa } from 'element-plus/es/locale';

let dialogVisible = ref(false);

// emits
let emits = defineEmits(['submit']);

let requestParamStart = {
    id: null,
    newStock: null,
    oldStock: null,
    // 备注
    remark: null,
}

let requestParam = ref(

    JSON.parse(JSON.stringify(requestParamStart))

);


let bodyData = ref({
    "gmtCreate": null,
    "gmtModified": null,
    "goods": {
        "barCode": null,
        "brandId": null,
        "children": [],
        "code": null,
        "descriptionJson": null,
        "fileIdOfImgArrStr": null,
        "gmtCreate": null,
        "gmtModified": null,
        "id": 3,
        "minOrder": null,
        "name": "测试1.2",
        "numberStr": null,
        "parent": {
            "barCode": "1111",
            "brandId": null,
            "children": null,
            "code": null,
            "descriptionJson": null,
            "fileIdOfImgArrStr": "[]",
            "gmtCreate": null,
            "gmtModified": 1698821041969,
            "id": 2,
            "minOrder": null,
            "name": "测试3",
            "numberStr": "1111",
            "parent": null,
            "parentId": null,
            "price": null,
            "priority": null,
            "purchasePrice": null,
            "retailPrice": null,
            "selectList": [],
            "stockWarningLower": null,
            "stockWarningUpper": null,
            "type": null,
            "typeId": null,
            "unitId": null,
            "wholesalePrice": null
        },
        "parentId": 2,
        "price": null,
        "priority": null,
        "purchasePrice": null,
        "retailPrice": null,
        "selectList": [],
        "stockWarningLower": null,
        "stockWarningUpper": null,
        "type": null,
        "typeId": null,
        "unitId": null,
        "wholesalePrice": null
    },
    "goodsId": 3,
    "id": 5,
    "stock": 3,
    "warehouse": {
        "empIdOfManager": null,
        "empOfManager": null,
        "gmtCreate": 1698739908505,
        "gmtModified": 1698739908505,
        "id": 477607964741637,
        "name": "3号仓库",
        "remark": null,
        "store": null,
        "storeId": null
    },
    "warehouseId": 477607964741637
});


let getById = () => {
    $kt.request.request({
        url: '/business-goods-web/admin/goodsWarehouseStock/getById',
        method: 'post',
        data: {
            id: requestParam.value.id,
        }
    }).then((res) => {
        if (res.data.success) {
            bodyData.value = res.data.data;
            requestParam.value.oldStock = bodyData.value.stock;
            ElNotification({
                title: '库存盘点',
                message: "当前库存：" + bodyData.value.stock,
            })
            return false;
        }
    });
}

/**
 * 打开弹窗
 */
let open = (id: any) => {
    dialogVisible.value = true;
    requestParam.value.id = id;
    getById();
}






let submit = () => {
    if (requestParam.value.newStock == null) {
        ElMessage({
            message: '请输入盘点数量',
            type: 'error',
        })
        return false;
    }

    const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })

    $kt.request.request({
        url: '/business-goods-web/admin/goodsWarehouseStock/check',
        method: 'post',
        data: requestParam.value,
    }).then((res) => {
        if (res.data.success) {
            ElMessage({
                message: '保存成功',
                type: 'success',
            })
            dialogVisible.value = false;

            loading.close();
            // 提交
            emits('submit');
            requestParam.value = JSON.parse(JSON.stringify(requestParamStart));
            dialogVisible.value = false;
            return false;
        }
        ElMessage({
            message: res.data.errMsg,
            type: 'error',
        });
        if (res.data.stateCode == "stockNotMatch") {
            requestParam.value.newStock = null;
            getById();
        }

        loading.close();
    });



}



// 暴露给父组件的方法
defineExpose({
    open
})


</script>


<style lang="scss" scoped>
.img-uploaded {
    cursor: pointer;
}

.img-uploaded:hover {
    opacity: 0.8;
}
</style>